﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using MxWeiXinPF.BLL;
namespace MxWeiXinPF.Web.admin.wxapp
{
    public partial class wxappEdit : Web.UI.ManagePage
    {
        int id = 0;
        private string action = MXEnums.ActionEnum.Add.ToString(); //操作类型

        wxapp_appinfo appinfoBll = new wxapp_appinfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = MyCommFun.RequestInt("id", 0);

            if (id != 0 && !appinfoBll.Exists(id))
            {
                JscriptMsg("记录不存在或已被删除！", "back");
                return;
            }

            if (!Page.IsPostBack)
            {
                BindWxappList();
                BindPicList();
                ShowInfo();


            }

        }


        #region 赋值操作=================================


        private void BindWxappList()
        {
            IList<Model.wxapp_appinfo> appinfolist = new List<Model.wxapp_appinfo>();
            appinfolist = appinfoBll.GetModelList("1=1");
            this.ddlWxAppList.Items.Clear();
            this.ddlWxAppList.Items.Add(new ListItem("==请选择小程序==", ""));

            foreach (Model.wxapp_appinfo appinfo in appinfolist)
            {

                this.ddlWxAppList.Items.Add(new ListItem(appinfo.wxappName, appinfo.id.ToString()));
            }


            ddlWxAppList.DataBind();
            if (id == 0)
            {
                return;
            }
            else
            {
                this.ddlWxAppList.SelectedValue = id.ToString();

            }

        }

        /// <summary>
        /// 绑定示例图
        /// </summary>
        private void BindPicList()
        {
            if (id == 0)
            {
                return;
            }

            IList<Model.mx_attachInfo> attachlist = new List<Model.mx_attachInfo>();
            BLL.mx_attachInfo attachBll = new mx_attachInfo();
            attachlist = attachBll.GetModelList("wxapp_appinfo_shili", "wxapp_appinfo", id);

            rptAlbumList.DataSource = attachlist;
            rptAlbumList.DataBind();

        }


        private void ShowInfo()
        {

            if (id == 0)
            {
                return;
            }


            Model.wxapp_appinfo appinfo = appinfoBll.GetModel(id);
            this.txtwxappRequirement.Text = appinfo.wxappRequirement;
            this.txttyQRPic.Text = appinfo.tyQRPic;
            imgtyQRPic.ImageUrl = appinfo.tyQRPic;
            this.txtwxappBrief.Text = appinfo.wxappBrief;
            this.txtoldPrice.Text = appinfo.oldPrice.ToString();
            this.txtcurrPrice.Text = appinfo.currPrice.ToString();
            this.ddlValidYear.SelectedValue = appinfo.validYear.ToString();
            rblStatus.SelectedValue = appinfo.status.ToString();


        }

        #endregion



        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            Model.wxapp_appinfo appinfo = new Model.wxapp_appinfo();

            #region  //先判断
            string strErr = "";
            if (this.ddlWxAppList.SelectedItem.Value == null)
            {
                strErr += "请选择一个小程序！";
            }
            if (this.txtwxappRequirement.Text.Trim().Length == 0)
            {
                strErr += "查看要求不能为空！";
            }


            if (strErr != "")
            {
                JscriptMsg(strErr, "back");
                return;
            }

            #endregion

            #region 赋值

            appinfo = appinfoBll.GetModel(id);
            int oldStatus = appinfo.status.Value;
            appinfo.wxappRequirement = txtwxappRequirement.Text.Trim();
            appinfo.tyQRPic = txttyQRPic.Text.Trim();
            appinfo.wxappBrief = txtwxappBrief.Text.Trim();
            appinfo.oldPrice = MyCommFun.Str2Decimal(txtoldPrice.Text);
            appinfo.currPrice = MyCommFun.Str2Decimal(txtcurrPrice.Text);
            appinfo.validYear = MyCommFun.Str2Int(ddlValidYear.SelectedItem.Value);
            appinfo.status = MyCommFun.Str2Int(rblStatus.SelectedItem.Value);

            if (oldStatus != appinfo.status && appinfo.status == 1)
            {
                appinfo.shelfTime = DateTime.Now;
            }
            appinfo.updateTime = DateTime.Now;


            #endregion

            //1修改主表
            appinfoBll.Update(appinfo);
            SavePicList();


            AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "设置小程序成功，主键为" + id); //记录日志
            JscriptMsg("设置小程序成功成功！", "wxapplist.aspx");

        }

        /// <summary>
        /// 保存示例图
        /// </summary>
        /// <param name="id"></param>
        private void SavePicList()
        {
            BLL.mx_attachInfo attachBll = new mx_attachInfo();
            attachBll.DeleteByRefTableInfo("wxapp_appinfo", "wxapp_appinfo_shili", id);
            Model.mx_attachInfo attachinfo = new Model.mx_attachInfo();

            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
            if (albumArr != null && albumArr.Length > 0)
            {

                for (int i = 0; i < albumArr.Length; i++)
                {
                    attachinfo = new Model.mx_attachInfo();
                    string[] imgArr = albumArr[i].Split('|');
                    // 添加 
                    attachinfo.refTableName = "wxapp_appinfo";
                    attachinfo.usedForCode = "wxapp_appinfo_shili";
                    attachinfo.usedForName = "微信小程序事例图";
                    attachinfo.refTableId = id;

                    attachinfo.attachUri = imgArr[1];
                    attachinfo.remark = MyCommFun.ObjToStr(remarkArr[i]);
                    attachinfo.sort_id = i;
                    attachinfo.createTime = DateTime.Now;
                    attachBll.Add(attachinfo);

                }

            }

        }




        protected void ddlWxAppList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int appinfoId = MyCommFun.Str2Int(this.ddlWxAppList.SelectedItem.Value);
            JscriptMsg("切换小程序！", "wxappEdit.aspx?id=" + appinfoId);
        }

    }
}